<template>
	<view>
		<uni-popup ref="show_choose_upload" type="bottom" :custom="true">
			<view class="choose-upload-popup f-mini-title c-title ">
				<view class="item-list fill-base radius-10">
					<view @tap.stop="toCheckItem('camera')" class="item-child flex-center">拍摄</view>
					<view @tap.stop="toCheckItem('album')" class="item-child flex-center b-1px-t" v-if="type === 1">
						从相册选择
					</view>
				</view>
				<view class="item-child flex-center fill-base mt-md mb-md text-bold radius-10"
					@tap.stop="$refs.show_choose_upload.close()">取消</view>
				<view class="space-safe"></view>
			</view>
		</uni-popup>
		<!-- #ifdef APP-PLUS -->
		<longbingbing-app-check-auth :type="checkType" ref="app_check_item"
			@confirm="toConfirmUpload(checkType)"></longbingbing-app-check-auth>
		<!-- #endif -->
	</view>
</template>

<script>
	import permision from '@/utils/permission.js'
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	export default {
		components: {},
		props: {
			type: {
				type: String | Number,
				default () {
					return 1
				}
			},
		},
		mounted() {},
		data() {
			return {
				checkType: 'upload'
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
		}),
		methods: {
			...mapActions(['getConfigInfo', 'getUserInfo']),
			...mapMutations(['updateUserItem']),
			open() {
				this.$refs.show_choose_upload.open()
			},
			close() {
				this.$refs.show_choose_upload.close()
			},
			async toCheckItem(type) {
				// #ifdef APP-PLUS
				if (plus.os.name == 'Android' && ((type === 'camera' && plus.navigator.checkPermission(
							'android.permission.CAMERA') ===
						'undetermined') || (type === 'album' && plus.navigator.checkPermission(
						'android.permission.READ_EXTERNAL_STORAGE') === 'undetermined'))) {
					this.checkType = type
					this.close()
					this.$refs.app_check_item.open()
				} else {
					this.toConfirmUpload(type)
				}
				// #endif
			},
			async toConfirmUpload(type) {
				this.close()
				let typeArr = {
					camera: 'android.permission.CAMERA',
					album: 'android.permission.READ_EXTERNAL_STORAGE'
				}
				let status = await permision.requestAndroid(typeArr[type])
				if (status === null || status === 1) {
					status = 1
				}
				if (status === 1) {
					this.$emit('confirm', [type])
					return
				}
				if (status === -1) {
					permision.gotoAppSetting()
					return
				}
			}
		},
	}
</script>

<style scoped lang="scss">
	.choose-upload-popup {
		width: 100%;

		.item-list {
			width: 100%;
		}

		.item-child {
			height: 90rpx;
		}
	}
</style>